home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 41.zip / BS1 part 41 / parallel-C disk .adf / linkio.c < prev    next >
C/C++ Source or Header  |  1997-12-31  |  10KB  |  376 lines

  1. /*************************************************************************
  2. *                                                                        *
  3. *  OCS Server  INMOS B004 Link Adaptor Driver                            *
  4. *                                                                        *
  5. *  12th March 1987.                                                      *
  6. *                                                                        *
  7. *  Copyright INMOS Limited, 1987.                                        *
  8. *                                                                        *
  9. *  Amiga Version by Juergen Sang & Robert Sang                           *
  10. *                                                                        *
  11. *  14th March 1988.                                                      *
  12. *                                                                        *
  13. *  Upgrade to 3L-Server by Gerhard Bartz                                 *
  14. *                                                                        *
  15. *  18th April 1989                                                       *
  16. *                                                                        *
  17. *************************************************************************/
  18. #include <stdio.h>
  19. #include "srvconst.h"
  20. #include "findboard.inc"
  21.  
  22. #define LINK_READ_OFFSET         0x09L
  23. #define LINK_WRITE_OFFSET        0x0BL
  24. #define LINK_IN_STATUS_OFFSET    0x0DL
  25. #define LINK_OUT_STATUS_OFFSET   0x0FL
  26. #define LINK_RESET_OFFSET        0x01L
  27. #define LINK_ERROR_OFFSET        0x01L
  28. #define ID_ADDRESS                0xffff000e
  29. #define PC_ID                     -1
  30. #define RESET_COUNT              10000
  31. #define ANALYSE_COUNT            10000
  32. #define MAX_LINK_TIME            32000
  33. char 
  34.    *link_base, *link_read, *link_write, *link_in_status,
  35.         *link_out_status, *link_reset, *link_error;
  36.  
  37. void init_root ()
  38. {
  39.    link_base=find_board(0x2200,1);
  40.    if (link_base==0L) {
  41.       printf("ERROR: SANG- BOARD NOT FOUND !!!\n");
  42.       exit(1);
  43.    }
  44.    link_read=link_base+LINK_READ_OFFSET;
  45.    link_write=link_base+LINK_WRITE_OFFSET;
  46.    link_in_status=link_base+LINK_IN_STATUS_OFFSET;
  47.    link_out_status=link_base+LINK_OUT_STATUS_OFFSET;
  48.    link_reset=link_base+LINK_RESET_OFFSET;
  49.    link_error=link_base+LINK_ERROR_OFFSET;
  50. }
  51.  
  52. int link_in_test ()
  53. /* Return TRUE if a byte is available on the link, FALSE otherwise. */
  54. {
  55.     return (((*link_in_status) & BIT_0) != 0);
  56. }
  57.  
  58. int byte_from_link ()
  59. /* Read a byte from the link adaptor. */
  60. {
  61.     while (!((*link_in_status) & BIT_0))
  62.         ;
  63.     return ((int)(*link_read));
  64. }
  65.  
  66. void test_byte_to_link(ch)
  67. int ch;
  68.  
  69. {
  70.     int i=5;
  71.  
  72.     *link_reset=1;
  73.     while((i--)>0);
  74.     i=1;
  75.     *link_reset=0;
  76.     while((i--)>0);
  77.     *link_write=(char)ch;
  78. }
  79.  
  80. void byte_to_link (ch)
  81. int ch;
  82. /* Write a byte to the link adaptor. */
  83. {
  84.     while (!((*link_out_status) & BIT_0))
  85.         ;
  86.     *link_write= (char)ch;
  87. }
  88.  
  89. int byte_to_link_t (ch)
  90. int ch;
  91. /* Write a byte to the link adaptor within a specified time, or timeout.
  92.    Return TRUE if the byte was NOT written, FALSE if the byte was written. */
  93. {
  94.     register int not_written = TRUE, i;
  95.     for (i=0; ((i < MAX_LINK_TIME) && not_written); i++)
  96.         if ((*link_out_status) & BIT_0) {
  97.             *link_write= (char)ch;
  98.             not_written = FALSE;
  99.         }
  100.     return (not_written);
  101. }
  102.  
  103. int error_test ()
  104. /* Test if an error is present in the transputer system. */
  105. {
  106.     return (!((*link_error) & BIT_0));
  107. }
  108.  
  109. void reset_root ()
  110. /* Reset the root transputer. */
  111. {
  112.     register int i;
  113.     *link_reset= 0;                     /* deassert reset   */
  114.     for (i=0; i < RESET_COUNT; i++)     /* wait awhile      */
  115.         ;
  116.     *link_reset= 1;                     /* assert reset     */
  117.     for (i=0; i < RESET_COUNT; i++)     /* wait awhile      */
  118.         ;
  119.     *link_reset= 0;                     /* deassert reset   */
  120. }
  121.  
  122. void reset_analyse_root ()
  123. /* Reset the root transputer into analyse mode. */
  124. {
  125.     register int i;
  126.     *link_reset= 0;                     /* deassert reset   */
  127.     for (i=0; i < RESET_COUNT; i++)     /* wait awhile      */
  128.         ;
  129.     *link_reset= 2;                     /* assert analyse   */
  130.     for (i=0; i < ANALYSE_COUNT; i++)   /* wait awhile      */
  131.         ;
  132.     *link_reset= 3;                     /* assert reset     */
  133.     for (i=0; i < RESET_COUNT; i++)     /* wait awhile      */
  134. ;
  135.     *link_reset= 0;                     /* deassert reset   */
  136.                               /* and analyse      */
  137. }
  138.  
  139. int word_from_link ()
  140. /* Read a word from the link. */
  141. /* This routine is host- and transputer- word-length dependent.
  142.    Here we assume a host int length of two bytes, and a transputer int length
  143.    of four bytes.
  144. */
  145. {
  146.     register int t, ch;
  147.     t = (byte_from_link()) & 0xff;         /* l.s. byte  */
  148.     ch =(byte_from_link()) & 0xff;        /* m.s. byte  */
  149.     t = t | ( ch<<8 );
  150.     ch = byte_from_link();        /* Ignore upper 16-bits sent by transputer */
  151.     ch = byte_from_link();
  152.     return (t);
  153. }
  154.  
  155. void word_to_link (w)
  156. /* Write a word to the link, least significant byte first. */
  157. /* This routine is host- and transputer- word-length dependent.
  158.    Here we assume a host int length of two bytes, and a transputer int length
  159.    of four bytes.
  160. */
  161. int w;
  162. {
  163.     byte_to_link ((int)(w & 0xff));         /* l.s. byte */
  164.     byte_to_link ((int)((w >> 8) & 0xff));  /* m.s. byte */
  165.     if (w < 0)
  166.     {
  167.         byte_to_link (0xff);
  168.         byte_to_link (0xff);
  169.     } else
  170.     {
  171.         byte_to_link (0);
  172.         byte_to_link (0);
  173.     }
  174. }
  175.  
  176. void slice_from_link (length, slice)
  177. int *length;
  178. char *slice;
  179. /* Read a slice from the link.
  180.    The slice consists of a four-byte length followed by a sequence of bytes. */
  181. {
  182.     register int i, real_len;
  183.     *length = real_len = word_from_link();
  184. #ifdef REV_A_FIX
  185.     if (real_len == 1) real_len = 2;
  186. #endif
  187.     for (i = real_len; i>0; i--)
  188.         *slice++ = byte_from_link();
  189. }
  190.  
  191. int trunc_slice_from_link (max_length, str)
  192. int max_length;
  193. char *str;
  194. /* Read a slice from the link whose maximum length is max_length.
  195.    If the length read in is longer than this, the remainder of the slice is
  196.    read in, but ignored. Return number of bytes read in. */
  197. {
  198.     int length;
  199.     register int real_len;
  200.     length = real_len = word_from_link();
  201. #ifdef REV_A_FIX
  202.     if (real_len == 1) real_len = 2;
  203. #endif
  204.     if (real_len > max_length)
  205.     {
  206.         register int i;
  207.         for (i = 0; i < max_length; i++)
  208.             *str++ = (char)byte_from_link();
  209.         for (i = max_length; i < real_len; i++)
  210.             byte_from_link();
  211.     } else
  212.     {
  213.         register int i;
  214.         for (i = 0; i < real_len; i++)
  215.             *str++ = (char)byte_from_link();
  216.     };
  217.     return (length);
  218. }
  219.  
  220. void slice_to_link (length, slice)
  221. int length;
  222. char *slice;
  223. /* Write a slice to the link.
  224.    The slice consists of a four-byte length followed by a sequence of bytes. */
  225. {
  226.     register int i, real_len = length;
  227.     word_to_link (real_len);
  228. #ifdef REV_A_FIX
  229.     if (real_len == 1) real_len = 2;
  230. #endif
  231.     for (i = 0; i < real_len; i++)
  232.         byte_to_link (*slice++);
  233. }
  234.  
  235. void long_word_to_link (w)
  236. /* Write a long word to the link. */
  237. /* This routine is host- and transputer- word-length dependent.
  238.    Here we assume a host long int length of at least four bytes,
  239.    and a transputer int length of four bytes.
  240. */
  241. long int w;
  242. {
  243.     register int i;
  244.     for (i = 0; i<4; i++)
  245.     {
  246.         byte_to_link ((int)(w & 0xff));
  247.         w >>= 8;
  248.     };
  249. }
  250.  
  251. long long_word_from_link ()
  252. /* Read a long word from the link. */
  253. /* This routine is host- and transputer- word-length dependent.
  254.    Here we assume a host long int length of at least four bytes,
  255.    and a transputer int length of four bytes.
  256. */
  257. {
  258.     long int result = 0;
  259.     register int i;
  260.     result |= (((long) (unsigned char) byte_from_link()) << 0);
  261.     result |= (((long) (unsigned char) byte_from_link()) << 8);
  262.     result |= (((long) (unsigned char) byte_from_link()) <<16);
  263.     result |= (((long) (unsigned char) byte_from_link()) <<24);
  264.     return (result);
  265. }
  266.  
  267. void long_slice_to_link (length, slice)
  268. long int length;
  269. char *slice;
  270. /* Write a slice to the link.
  271.    The slice consists of a four-byte length followed by a sequence of bytes. */
  272. {
  273.     long int i, real_len = length;
  274.     long_word_to_link (real_len);
  275. #ifdef REV_A_FIX
  276.     if (real_len == 1) real_len = 2;
  277. #endif
  278.     for (i = 0; i < real_len; i++)
  279.         byte_to_link (*slice++);
  280. }
  281.  
  282. void long_slice_from_link (length, slice)
  283. long int *length;
  284. char *slice;
  285. /* Read a slice from the link.
  286.    The slice consists of a four-byte length followed by a sequence of bytes. */
  287. {
  288.     long int i, real_len;
  289.     *length = real_len = long_word_from_link();
  290. #ifdef REV_A_FIX
  291.     if (real_len == 1) real_len = 2;
  292. #endif
  293.     for (i = real_len; i>0; i--)
  294.         *slice++ = byte_from_link();
  295. }
  296.  
  297. void safe_byte_to_link ( b )
  298. int b;
  299. {
  300.     byte_to_link ( b );
  301. #ifdef REV_A_FIX
  302.     byte_to_link ( b );
  303. #endif
  304. }
  305.  
  306. int safe_byte_from_link ()
  307. {
  308.     int b, padding;
  309.     b = byte_from_link ();
  310. #ifdef REV_A_FIX
  311.     padding = byte_from_link ();
  312. #endif
  313.     return (b);
  314. }
  315.  
  316. int boot_root (boot_file_name)
  317. char *boot_file_name;
  318. /**** SUBTRACT1 begin
  319. #define BOOT_BUFFER_LENGTH RECORD_LENGTH
  320.       SUBTRACT1 end ****/
  321. /* Boot the root transputer, ie. reset the root transputer, open the 
  322.    boot file  and write this down the link adaptor as a stream of bytes. */
  323. {
  324.     int result = FALSE;
  325.     FILE *boot_file;
  326.     if ((boot_file = fopen (boot_file_name, "r")) != NULL ) {
  327. register int count = 0;
  328. int anzahl=0;
  329. char file_buffer [BOOT_BUFFER_LENGTH];
  330. int do_boot = TRUE;
  331. while (do_boot)
  332. {
  333. count = fread ( file_buffer, sizeof(char), BOOT_BUFFER_LENGTH, boot_file );
  334. if (count > 0)
  335. {
  336. register int i;
  337. #ifdef DEBUG
  338. for (i = 0; ((i < count) && do_boot); i++)
  339. {
  340.     do_boot = ! byte_to_link_t (file_buffer[i]);
  341.     if (!do_boot) printf("Fehler bei Byte:%d\n",anzahl);
  342.    anzahl++;
  343. }
  344. #else
  345. register char* ptr;
  346. ptr=file_buffer;
  347. for (i = 0; i<count; i++)
  348. {
  349.     while(!((*link_out_status)&BIT_0));
  350.     *link_write=*(ptr+i);
  351. }
  352. #endif
  353.  
  354. if ( ! do_boot )
  355.     result = OPERATIONFAILED_ERR;
  356. }
  357. else
  358. {
  359. do_boot = FALSE;
  360. if feof (boot_file)
  361. {
  362.     if (fclose (boot_file) == 0)
  363.         result = F_OK;
  364.     else
  365.         result = OPERATIONFAILED_ERR;
  366. }
  367. else
  368.     result = OPERATIONFAILED_ERR;
  369. };
  370. };
  371.     }
  372.     else
  373.         result = OPERATIONFAILED_ERR;
  374.     return (result);
  375. }
  376.